import axios from 'axios'
import {header as headerFun,progressEvent as progressEventFun,startHeader} from './axiosTypeNorm'
axios.interceptors.request.use(success => {
  console.log(success)
  var arr:string[] = []
  document.cookie.split(';').map(res => {
    arr = arr.concat(res.split('='))
  })
  success.headers['token'] = arr[arr.length - 1]
  return success
}, err => {
  console.log(err)
  return err
})
axios.interceptors.response.use(success => {
  // console.log('header',success.headers)
  document.cookie = `token=${success.headers['setcookie']}`
  return success 
  // 此处的数据 是全局调用axios的结构 你可以做处理 比如后端用比较复杂的格式
  // 你可以在这里把复杂的格式 处理成简单的
}, err => {
  if(err && err.response){
    switch(err.response.status){
      case 400:
        console.log("错误请求")
      break
      case 401:
        console.log("未授权，请重新登录")
      break
      case 403:
        console.log("拒绝访问")
      break
      case 404:
        console.log("请求错误，未找到该资源")
      break
      case 408:
        console.log("请求超时")
      break
      case 500:
        console.log("服务器端出错")
      break
      case 501:
        console.log("网络未实现")
      break
      case 502:
        console.log("网络错误")
      break
      case 503:
        console.log("服务不可用")
      break
      case 504:
        console.log("网络超时")
      break
      case 505:
        console.log("http版本不支持该请求")
      break
      default:
        console.log(`连接错误${err.response.status}`)
    }
  }else{
    console.log('连接到服务器失败')
  }
  return err
})
let cancelToken = axios.CancelToken
/** 请求 限制自定义头*/
let axiosOne = axios.create({ // 实例化 用于多种请求
  timeout: 3000,
  url: '',
  baseURL: '',
  method: 'get',
  params: '',
  data: '',
  responseType: 'json',
  cancelToken: new cancelToken(function(){}),
  headers: {
    name: 1
  },
  withCredentials: false,
  onUploadProgress: (progressEvent:progressEventFun) => {
    let complete = (progressEvent.loaded / progressEvent.total * 100 | 0) + '%'
    console.log('123',complete)
  }
})
class Http {
  cancelToken:boolean = false // false不取消发送 true 取消发送
  self = this
  header:startHeader = {
    timeout: 3000,
    url: '',
    baseURL: '',
    method: 'get',
    params: '',
    data: '',
    responseType: 'json',
    cancelToken: new cancelToken(function(){}),
    headers: {
      name: 1
    },
    withCredentials: false,
    onUploadProgress: (progressEvent:progressEventFun) => {
      let complete = (progressEvent.loaded / progressEvent.total * 100 | 0) + '%'
      console.log('123',complete)
    }
  }
  get:(url:string, data:any, cancal:boolean)=> void
  post:(url:string, data:any, cancal:boolean)=> void
  fileupload:(url:string, data:any, cancal:boolean)=> void
  fileUploadProgress:(url:string, data:any,header:headerFun, cancal:boolean)=> void
  constructor() {
    this.get = this.getInfo
    this.post = this.postInfo
    this.fileupload = this.fileuploadInfo
    this.fileUploadProgress = this.fileProgress
  }
  cancal() {
    this.header.cancelToken = new cancelToken(function (res:Function) {
      res('取消发送')
    })
  }
  fileProgress(url:string, data:any, header:headerFun,onUploadProgress:any,cancal = false) {
    if (cancal) {
      this.cancal()
    }
    let allHeader = JSON.parse(JSON.stringify(Object.assign({},{...this.header.headers},{...header})))
    let headers = {headers:{...allHeader,"content-type": "multipart/form-data"}}
    if(onUploadProgress) {
      this.header.onUploadProgress = onUploadProgress.onUploadProgress
    }
    return axios.post(url, data,Object.assign({},{...this.header},{...headers}))
  }
  getInfo(url:string, data:any, cancal = false) {
    if (cancal) {
      this.cancal()
    }
    this.header.url = url
    this.header.params = data
    this.header.method = 'get'
    return axios({
      ...this.header
    })
  }
  postInfo(url:string, data:any, cancal = false) {
    if (cancal) {
      this.cancal()
    }
    this.header.url = url
    this.header.data = data
    this.header.method = 'post'
    return axios({
      ...this.header
    })
  }
  fileuploadInfo(url:string, form:any, cancal = false) {
    if (cancal) {
      this.cancal()
    }
    this.header.url = url
    this.header.data = form
    this.header.method = 'post'
    this.header.responseType = 'json'
    return axios({
      ...this.header
    })
  }
}
const api = new Http()
export default api
